package com.i72.penetrate.init;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.i72.penetrate.blance.RPCBlance;
import com.i72.penetrate.blance.RandomBlance;
import com.i72.penetrate.rpc.HttpTarget;
import com.i72.penetrate.tools.HttpRequestHelper;
import com.i72.penetrate.touch.HttpTouchPlane;
import com.i72.penetrate.validate.FormValidate;
import com.i72.penetrate.validate.RPCValidate;
import com.i72.penetrate.web.PenetrateServlet;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;

@Conditional(PenetrateConfigurationHTTPMarket.class)
public class PenetrateConfigurationHTTPInit {

    @Bean
    public HttpTarget httpTarget(){
        return new HttpTarget();
    }

    @Bean
    public HttpTouchPlane httpTouchPlane(){
        return new HttpTouchPlane();
    }

    @Bean
    public RestTemplate getRestTemplate(){
        OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
        factory.setConnectTimeout(15000); // 连接超时
        factory.setReadTimeout(5000); // 数据读取超时时间
        factory.setWriteTimeout(5000);
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().clear(); //干掉其他的处理器，只保留json 因为默认有14个，浪费性能，只采用fastjson的即可

        FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(new MediaType("application", "json")); //就是这么逆天 具体看MediaType的static部分
        httpMessageConverter.setSupportedMediaTypes(mediaTypes);


        restTemplate.getMessageConverters().add(httpMessageConverter);
        return restTemplate;
    }

    @Bean
    public RPCBlance getBlance(){
        return new RandomBlance();
    }

    @Bean
    public RPCValidate getValidate(){ return new FormValidate();}




    @Bean
    public ServletRegistrationBean getServletRegistrationBean(PenetrateServlet servlet) {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>(servlet, "/*");
        servletRegistrationBean.setLoadOnStartup(-1);
        return servletRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean getServletListenerRegistrationBean(RequestContextListener listener){
        ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean();
        listenerRegistrationBean.setListener(listener);
        listenerRegistrationBean.setOrder(-1);
        return listenerRegistrationBean;
    }

    @Bean
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    }

    @Bean
    public PenetrateServlet getServlet(){
        return new PenetrateServlet();
    }


    @Bean
    //@Scope("prototype")
    @Scope("request")
    public HttpRequestHelper getHttpRequestHelper(HttpServletRequest request, HttpServletResponse response){
        return new HttpRequestHelper(request,response);
    }

    @Bean
    public Validator getValidator(){
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        return validatorFactory.getValidator();
    }

}
